import 'package:flutter/material.dart';
import 'package:fsharing/core/viewmodels/pages/mine/web_view_model.dart';
import 'package:fsharing/main.dart';
import 'package:fsharing/ui/widgets/background_container.dart';
import 'package:fsharing/ui/widgets/round_back_button.dart';
import 'package:provider/provider.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebViewPageArgs {
  final String title;
  final String subTitle;
  final String url;
  final bool query;

  WebViewPageArgs(this.title, this.subTitle, this.url, {this.query});
}

class WebViewPage extends StatelessWidget {
  final WebViewPageArgs args;

  WebViewPage({
    Key key,
    @required this.args,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => WebViewModel(),
      child: BackgroundContainer(
        child: Scaffold(
          appBar: AppBar(
            leading: RoundBackButton(),
            title: Text(args.title),
          ),
          body: Container(
            padding: EdgeInsets.symmetric(horizontal: 12),
            child: Column(
              children: <Widget>[
                Container(
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.only(left: 12),
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage('assets/images/mine/img_agreement_header.png'),
                    ),
                  ),
                  height: 50,
                  child: Text(args.subTitle),
                ),
                Expanded(
                  child: Theme(
                    data: lightTheme(),
                    child: Material(
                      child: Builder(
                        builder: (context) {
                          return WebView(
                            initialUrl: args.url,
                            onPageFinished: (url) {
                              _onPageFinished(context);
                            },
                          );
                        },
                      ),
                    ),
                  ),
                ),
                if (args.query ?? false)
                  Container(
                    margin: EdgeInsets.all(12),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        RaisedButton(
                          onPressed: () {
                            Navigator.of(context).pop(false);
                          },
                          color: Colors.grey,
                          child: Text('不同意'),
                        ),
                        Consumer<WebViewModel>(
                          builder: (context, model, child) {
                            return RaisedButton(
                              onPressed: model.finished
                                  ? () {
                                      Navigator.of(context).pop(true);
                                    }
                                  : null,
                              child: Text('同意'),
                            );
                          },
                        ),
                      ],
                    ),
                  ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void _onPageFinished(BuildContext context) {
    WebViewModel model = Provider.of(context, listen: false);
    model.finished = true;
  }
}
